import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DocumentParseTest {
    public static void main(String[] args) throws Exception {
        List<String> sqlList = new ArrayList<>();
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
//        Document parse = documentBuilder.parse("leetcode/src1/test.xml");
        //D:\myCode\sword-finger-offer\leetcode\src1\testcpt.cpt
        File file = new File("D:\\myCode\\sword-finger-offer\\leetcode\\src1\\testcpt.cpt");
        Document parse1 = documentBuilder.parse(file);
        NodeList nodeList = parse1.getElementsByTagName("Query");
        int length = nodeList.getLength();
        for (int i = 0; i < length; i++) {
            String sql = nodeList.item(i).getTextContent();
//            System.out.println(sql);
            sqlList.add(sql);
        }
        String str="from\\s+(.*)\\s+where?";
        String matchStr = "from\\s+(\\w+.{1}\\w+)\\s+";
        String matchStr2 = "from\\s+(\\w+\\.\\w+)\\s+";
        String matchStr3 = "join\\s+(\\w+\\.\\w+)\\s+";
        String matchStr1 = "from\\s+(w*)\\s+";
        Pattern p=Pattern.compile(matchStr2);
        Pattern p1=Pattern.compile(matchStr3);
        for (String sql : sqlList) {
            Matcher matcher=p.matcher(sql);
            Matcher matcher1 = p1.matcher(sql);
            while (matcher.find()) {
                String string =matcher.group(1);
                System.out.println(string);
            }
            while (matcher1.find()){
                String string =matcher1.group(1);
                System.out.println(string);
            }
        }

//        System.out.println(parse1.getElementsByTagName("Query").item(1).getTextContent().toString());
    }
}
